#include "includes.h"

//***********************ADC *//////////////////////


void adc_init(unsigned char channel)//functia care initializeaza registrii folositi de convertor
{
//ADMUX = (1<<REFS0)|(1<<REFS1)|channel;//ADC voltage reference set to internal 2.56V  MUX0 set for reading input from PA1
ADMUX = (1<<REFS0)|channel;//ADC voltage reference set to AVCC

ADCSRA |=(1<<ADEN); // se seteaza ADEN pentru a activa convertorul analogic-digital
}

unsigned short citeste_adc(void) //functia care citeste rezultatele conversiilor
{
	unsigned char adc_low,adc_high;
    ADCSRA|=(1<<ADSC);
    while(ADCSRA & 0x40); // wait for the end of the conversion
    _delay_ms(30);
	
    adc_low = ADCL; //partea nesemnificativa a rezultatului pe care nu o vom folosi
	adc_high = ADCH;
   return (adc_low|(adc_high<<8));//partea semnificativa a rezultatului pe care o vom folosi pentru afisarea temperaturii
}




void afiseaza_adc(void)
{
    
	unsigned short rez;
	float decimal_rez;
    char buffer[4];
	
    rez = citeste_adc();
	decimal_rez = rez * 0.0048828125;//ADC reference set to AVCC - precision of 4.88mV/LSB
	//decimal_rez = rez * 0.0025;//ADC reference set to 2.56 - internal voltage reference --- precision of 2.5mV/LSB
	rez=decimal_rez * 1000;
	itoa(rez,buffer,10);
	lcd_puts(buffer);
	
         
}
